﻿using Gma.System.MouseKeyHook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ClickScreen
{
    internal class MouseKeyHookHelper
    {


        private static IKeyboardMouseEvents mk_hook;
        private bool _HookIsInited = false;

        public event Action<System.Drawing.Point> SetMouseDownPointEvent;

        public async Task InstallHookAsync()
        {
            if (this._HookIsInited)
            {
                return;
            }
            await Application.Current.Dispatcher.InvokeAsync(new Action(() =>
            {
                InitHook();
            }));
        }

        /// <summary>
        /// init keyboard hook
        /// </summary>
        /// <param name="retry">递归次数</param>
        private void InitHook(int retry = 0)
        {
            try
            {
                this.UnInitHook();
                retry++;
                if (retry > 3)
                {
                    System.Threading.Thread.Sleep(1000);
                    return;
                }
                mk_hook = Hook.GlobalEvents();
                mk_hook.KeyDown += GlobalHook_KeyDown;
                //mk_hook.MouseDown += GlobalHook_MouseDown;
                //mk_hook.Start(true);
                this._HookIsInited = true;
            }
            catch (Exception ex)
            {
                this.UnInitHook();
                this.InitHook(retry);
            }
        }

        /// <summary>
        /// uninit keyboard hook
        /// </summary>
        private void UnInitHook()
        {
            try
            {
                this._HookIsInited = false;
                if (mk_hook == null)
                {
                    return;
                }
                mk_hook.KeyDown -= GlobalHook_KeyDown;
                //mk_hook.MouseDown -= GlobalHook_MouseDown;
                //mk_hook.MouseMove -= GlobalHook_MouseMove;
                //mk_hook.Stop();
                mk_hook.Dispose();
            }
            catch (Exception ex)
            {
                //Log.Error($"UnInit MouseKeyboardHook failed,error:{ex} MK_HookViewModel002");
            }
        }


        Key[] EndInputKeys = new Key[] {
            Key.Tab, Key.LeftAlt , Key.RightAlt , Key.Enter , Key.Up, Key.Down, Key.Left , Key.Right, Key.PageUp , Key.PageDown
        };
        private void GlobalHook_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            //Enum.TryParse<Key>(e.KeyData.ToString(), out Key key);

            if (e.KeyData == System.Windows.Forms.Keys.Escape)
            {
                UnInitHook();
            }
            else if (e.KeyData == System.Windows.Forms.Keys.LShiftKey)
            {
                System.Drawing.Point pos = GetMousePose();
                SetMouseDownPointEvent?.Invoke(pos);
            }
        }
        private void GlobalHook_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            System.Drawing.Point pos = new System.Drawing.Point(e.X, e.Y);

        }

        [System.Runtime.InteropServices.DllImport("user32.dll")] //导入user32.dll函数库
        public static extern bool GetCursorPos(out System.Drawing.Point lpPoint);//获取鼠标坐标

        private System.Drawing.Point GetMousePose()
        {
            System.Drawing.Point mp = new System.Drawing.Point();
            GetCursorPos(out mp);
            int mousex = mp.X;  //鼠标当前X坐标
            int mousey = mp.Y;  //鼠标当前Y坐标
            return mp;
        }

    }
}
